#include <iostream> 
#include <algorithm> 
#include <cstring> 
using namespace std; 
const int N = 1e5 + 10, M = 1e9 + 10; 
const long long INF = 1e18; 
int n, m; 
int L[N], R[N]; 
long long dp[N][M]; 

int main() { 
    cin >> n >> m; 
    for (int i = 1; i <= n; i++) { 
        cin >> L[i] >> R[i]; 
    } 
    for (int i = 0; i <= n; i++) { 
        for (int j = 1; j <= m; j++) { 
            dp[i][j] = INF; 
        } 
    } 
    dp[0][1] = 0; 
    for (int i = 1; i <= n; i++) { 
        for (int j = L[i]; j <= R[i]; j++) { 
            for (int k = L[i - 1]; k <= R[i - 1]; k++) { 
                dp[i][j] = min(dp[i][j], dp[i - 1][k] + abs(k - j)); 
            } 
        } 
    } 
    long long ans = INF; 
    for (int j = L[n]; j <= R[n]; j++) { 
        ans = min(ans, dp[n][j]); 
    } 
    cout << ans << endl; 
    return 0; 
}
